package com.nineclock.auth.processor.impl;

import com.nineclock.auth.entity.IntegrationAuthenticationEntity;
import com.nineclock.auth.processor.AuthenticationProcessor;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.system.dto.SysUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Component
public class UsernameAndPasswordAuthenticationProcessor implements AuthenticationProcessor {

    @Autowired
    SysUserFeign userFeign;

    /**
     * 获取 UserDto对象
     * @param entity
     * @return
     */
    @Override
    public SysUserDTO authenticate(IntegrationAuthenticationEntity entity) {
        //获取用户名 和密码信息
        String username = entity.getAuthParameter("username");
        String password = entity.getAuthParameter("password");
        if(username == null || password == null){
            throw new NcException(ResponseEnum.INVALID_USERNAME_PASSWORD);
        }

        SysUserDTO userDto = userFeign.queryUser(username).getData();
        if(userDto == null){
            throw new NcException(ResponseEnum.USER_NOT_FOUND);
        }

        return userDto;
    }

    /**
     * 验证 是否使用该 处理器
     * @param entity
     * @return
     */
    @Override
    public boolean support(IntegrationAuthenticationEntity entity) {
        if(entity != null && entity.getAuthType() == null ){
            return true;
        }
        return false;
    }
}
